package com.eastmelon.blog.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.eastmelon.basic.model.Pager;
import com.eastmelon.blog.dao.ArticleDao;
import com.eastmelon.blog.entity.Article;
import com.eastmelon.blog.service.ArticleService;
import com.eastmelon.blog.util.uploadUtil;
import com.eastmelon.blog.vo.DateCount;

@Service("articleService")
public class ArticleServiceImpl implements ArticleService {
	@Resource(name = "articleDao")
	private ArticleDao articleDao;

	@Override
	public Article get(Serializable id) {
		return articleDao.get(id);
	}

	@Override
	public void save(Article article, InputStream is) {
		try {
			if (is.available() != 0)
				uploadUtil.uploadImage(is, article.getArticlePic(), 400, 185);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		articleDao.save(article);
	}

	@Override
	public void update(Article article, InputStream is, String oldImageName) {
		try {
			if (is.available() != 0) {
				uploadUtil.uploadImage(is, article.getArticlePic(), 400, 185);
				if (oldImageName != null || !"".equals(oldImageName)) {
					uploadUtil.deleteImage(oldImageName);
				}
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		if (("".equals(article.getArticlePic()) || article.getArticlePic() == null)
				&& (!"".equals(oldImageName) || oldImageName != null)) {
			uploadUtil.deleteImage(oldImageName);
		}
		articleDao.update(article);
	}

	@Override
	public void deleteById(Serializable id) {
		String oldImageName = articleDao.getOldImage(id);
		if (oldImageName != null && !"".equals(oldImageName)) {
			uploadUtil.deleteImage(oldImageName);
		}
		articleDao.deleteById(id);
	}

	@Override
	public List<Article> find() {
		return articleDao.find(null);
	}

	@Override
	public void deleteByIds(Serializable[] ids) {
		List<String> oldImageNames = articleDao.findOldImages(ids);
		if (oldImageNames.size() > 0) {
			for (String name : oldImageNames) {
				if (name != null && !"".equals(name)) {
					uploadUtil.deleteImage(name);
				}
			}
		}
		articleDao.deleteByIds(ids);
	}

	@Override
	public String getOldImage(Serializable id) {
		return articleDao.getOldImage(id);
	}

	@Override
	public List<Article> findAll() {
		return articleDao.findAll();
	}

	@Override
	public Pager<Article> findPage() {
		return articleDao.findPage();
	}

	@Override
	public List<DateCount> findDateCount() {
		return articleDao.findDateCount();
	}

	@Override
	public Pager<Article> findByCategory(Serializable id) {
		return articleDao.findByCategory(id);
	}

	@Override
	public Pager<Article> findByTags(Serializable id) {
		return articleDao.findByTags(id);
	}

	@Override
	public Pager<Article> findByDateCategory(String name) {
		return articleDao.findByDateCategory(name);
	}

	@Override
	public Article getLastItem() {
		return articleDao.getLastItem();
	}

	@Override
	public Article getPreItem(Serializable id) {
		return articleDao.getPreItem(id);
	}

	@Override
	public Article getNextItem(Serializable id) {
		return articleDao.getNextItem(id);
	}
}
